﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Voluntariar.Sistema
{
    public static class Utils
    {
        #region Conversões de Consulta para o Objeto
        public static string ConverterString(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? data.ToString() : string.Empty;
        }
        public static decimal ConverterDecimal(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToDecimal(data) : 0;
        }
        public static int ConverterInt(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToInt32(data) : 0;
        }
        public static double ConverterDouble(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToDouble(data) : 0;
        }
        public static DateTime ConverterDateTime(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToDateTime(data) : new DateTime();
        }
        public static DateTime? ConverterDateTimeNull(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToDateTime(data) : new DateTime?();
        }
        public static bool ConverterBool(object data)
        {
            return !string.IsNullOrEmpty(data.ToString()) ? Convert.ToBoolean(data.ToString()) : false;
        }
        public static bool toBool(object data)
        {
            return Convert.ToBoolean(data);
        }
        #endregion
        #region Parametros de Consulta
        public static object Parametro(int i)
        {
            return i > 0 ? i : (object)DBNull.Value;
        }
        public static object Parametro(decimal i)
        {
            return i > 0 ? i : (object)DBNull.Value;
        }
        public static object Parametro(double i)
        {
            return i > 0 ? i : (object)DBNull.Value;
        }
        public static object Parametro(string s)
        {
            return !string.IsNullOrEmpty(s) ? s : (object)DBNull.Value;
        }
        public static object Parametro(DateTime? dt)
        {
            return dt != null ? (DateTime)dt : (object)DBNull.Value;
        }
        #endregion

        public static bool isCPFValido(this string cpf)
        {
            int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
            int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

            string tempCpf;
            string digito;
            int soma;
            int resto;

            cpf = cpf.Trim();
            cpf = cpf.Replace(".", "").Replace("-", "");

            if (cpf.Length != 11)
                return false;

            tempCpf = cpf.Substring(0, 9);
            soma = 0;

            for (int i = 0; i < 9; i++)
                soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];

            resto = soma % 11;

            if (resto < 2)
                resto = 0;
            else
                resto = 11 - resto;

            digito = resto.ToString();
            tempCpf = tempCpf + digito;
            soma = 0;

            for (int i = 0; i < 10; i++)
                soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];

            resto = soma % 11;

            if (resto < 2)
                resto = 0;
            else
                resto = 11 - resto;

            digito = digito + resto.ToString();

            return cpf.EndsWith(digito);
        }
    }
}
